Template.messages_notice.onCreated(function () {
  this.open = new ReactiveVar(false);
  this.type = new ReactiveVar();
  this.id = new ReactiveVar();

  this.autorun(() => {
    this.open.set(Session.get('class.messages.messagesNotice.open'));
    this.type.set(Session.get('class.messages.messagesNotice.type'));
    this.id.set(Session.get('class.messages.messagesNotice.id'));
  });
});

Template.messages_notice.helpers({
  open: function() {
    return Template.instance().open.get();
  },
  
  title: function() {
    let type = Template.instance().type.get();

    if (type == 'delete-message') {
      return '确定删除这条私信？';
    }
    
    if (type == 'delete-messages') {
      return '删除会话，这将删除所有聊天信息';
    }

    return '';
  }
});

Template.messages_notice.events({
  'click #close-messages-notice' () {
    Session.set('class.messages.messagesNotice.open', false);
  },

  'click #messages-notice-confirm' (e, instance) {
    let type = instance.type.get();

    if (type == 'delete-message') {
      Meteor.call('deleteMessage', { id: instance.id.get() }, (err, res) => {
        if (res) {
          Session.set('class.messages.messagesNotice.open', false);
        }
      });
    }
    
    if (type == 'delete-messages') {
      let options = {
        userId: Meteor.userId(),
        friendId: Session.get('class.messages.chatroomFriendId')
      };

      Meteor.call('deleteMessages', options, (err, res) => {
        if (res) {
          Session.set('class.messages.messagesNotice.open', false);
          Session.set('class.messages.modalContent', 'messages');
        }
      });
    }
  }
});